﻿using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.DataVisualization.Charting;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Threading;
using TYSXClientReport.Model;

namespace TYSXClientReport
{
    public partial class MainPage : UserControl
    {
        private VideoAdapterCollection vacClinet;
        private VideoAdapterCollection vacSourClinet;
        private VideoAdapterCollection vacWap;
        private VideoAdapterCollection vacSourWap;
        public MainPage()
        {
            InitializeComponent();

            DispatcherTimer dispatcherTimer = new DispatcherTimer();
            dispatcherTimer.Interval = TimeSpan.FromMilliseconds(2500);
            dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
            dispatcherTimer.Start();
        }

        private void dispatcherTimer_Tick(object sender, EventArgs e)
        {
            vacSourClinet.Sort(delegate(VideoAdapter va1, VideoAdapter va2) { return Comparer<string>.Default.Compare(va1.strDate, va2.strDate); });

            if (vacClinet[vacClinet.Count - 1].TerminalCount >= vacSourClinet[vacSourClinet.Count - 1].TerminalCount)
            {
                vacClinet.Clear();
                vacClinet = GetDataSourceFirst(vacSourClinet);
                BindingChart(mcChartClient, vacClinet, vacSourClinet[vacSourClinet.Count - 1].UserNum);

                vacWap.Clear();
                vacWap = GetDataSourceFirst(vacSourWap);
                BindingChart(mcChartWap, vacWap, vacSourWap[vacSourWap.Count - 1].UserNum);
                return;
            }
            for (int i = 1; i < vacSourClinet.Count; i++)
            {
                if (vacClinet[i - 1].UserNum == vacSourClinet[i - 1].UserNum && vacClinet[i].UserNum < vacSourClinet[i].UserNum)
                {
                    VideoAdapterCopy(vacSourClinet, vacClinet, i);
                    BindingChart(mcChartClient, vacClinet, vacSourClinet[vacSourClinet.Count - 1].UserNum);

                    VideoAdapterCopy(vacSourWap, vacWap, i);
                    BindingChart(mcChartWap, vacWap, vacSourWap[vacSourWap.Count - 1].UserNum);

                    break;
                }
            }
        }

        private void VideoAdapterCopy(VideoAdapterCollection sour, VideoAdapterCollection des, int index)
        {
            des[index].strDate = sour[index].strDate;
            des[index].UserNum = sour[index].UserNum;
            des[index].TerminalCount = sour[index].TerminalCount;
            des[index].UserPerCent = sour[index].UserPerCent;
        }

        /// <summary>
        /// 绑定Chart         X:strDate
        ///                   Y->"用户占比":UserPerCent,"用户数":UserNum,"终端数":TerminalCount
        /// </summary>
        /// <param name="chartSour"></param>
        /// <param name="vac"></param>
        private void BindingChart(Chart chartSour, VideoAdapterCollection vac, float fY)
        {
            #region Set Styles
            Style stylsPercent = new System.Windows.Style();
            stylsPercent.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterYellow = new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Red));
            stylsPercent.Setters.Add(setterYellow);

            Style styUsers = new System.Windows.Style();
            styUsers.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterBlue = new Setter(Control.BackgroundProperty, new SolidColorBrush(Colors.Blue));
            styUsers.Setters.Add(setterBlue);

            Style styTerminalCount = new System.Windows.Style();
            styTerminalCount.TargetType = typeof(System.Windows.Controls.Control);
            Setter setterGreen = new Setter(Control.BackgroundProperty,new SolidColorBrush(Colors.Green));
            styTerminalCount.Setters.Add(setterGreen);
            #endregion

            #region Bind chart
            chartSour.Series.Clear();

            ColumnSeries csUsers = new ColumnSeries();
            csUsers.Title = "用户数";
            csUsers.ItemsSource = vac;
            csUsers.IndependentValueBinding = new Binding("strDate");
            csUsers.DependentValueBinding = new Binding("UserNum");
            csUsers.DataPointStyle = styUsers;
            LinearAxis lac = new LinearAxis();
            lac.Orientation = AxisOrientation.Y;
            lac.Minimum = 0;
            lac.Maximum = (int)fY + 20;
            lac.Interval = 20;
            lac.ShowGridLines = true;
            csUsers.DependentRangeAxis = lac;
            chartSour.Series.Add(csUsers);

            ColumnSeries csTerminalCount = new ColumnSeries();
            csTerminalCount.Title = "终端数";
            csTerminalCount.ItemsSource = vac;
            csTerminalCount.IndependentValueBinding = new Binding("strDate");
            csTerminalCount.DependentValueBinding = new Binding("TerminalCount");
            csTerminalCount.Background = new SolidColorBrush(Colors.Blue);
            csTerminalCount.DataPointStyle = styTerminalCount;
            csTerminalCount.DependentRangeAxis = lac;
            chartSour.Series.Add(csTerminalCount);

            LineSeries lsPercent = new LineSeries();
            lsPercent.Title = "用户占比";
            lsPercent.ItemsSource = vac;
            lsPercent.IndependentValueBinding = new Binding("strDate");
            lsPercent.DependentValueBinding = new Binding("UserPerCent");
            lsPercent.DataPointStyle = stylsPercent;
            lsPercent.AllowDrop = true;
            LinearAxis la = new LinearAxis();
            la.Orientation = AxisOrientation.Y;
            la.Title = "用户占比(%)";
            la.Minimum = 0;
            la.Maximum = 100;
            la.Interval = 10;
            la.ShowGridLines = false;
            lsPercent.DependentRangeAxis = la;
            chartSour.Series.Add(lsPercent);

            #endregion
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            vacSourClinet = GetDataSourceClinet();
            vacClinet = GetDataSourceFirst(vacSourClinet);

            BindingChart(mcChartClient, vacClinet, vacSourClinet[vacSourClinet.Count-1].UserNum);

            vacSourWap = GetDataSourceWap();
            vacWap = GetDataSourceFirst(vacSourWap);

            BindingChart(mcChartWap, vacWap, vacSourWap[vacSourWap.Count - 1].UserNum);
        }

        private VideoAdapterCollection GetDataSourceClinet()
        {
            return new VideoAdapterCollection()
            {
                (new VideoAdapter { strDate = "2010-02", UserNum = 31.15f, TerminalCount = 9, UserPerCent = 15.59f }),
                (new VideoAdapter { strDate = "2010-03", UserNum = 60.09f, TerminalCount = 32, UserPerCent = 30.08f }),
                (new VideoAdapter { strDate = "2010-04", UserNum = 82.99f, TerminalCount = 44, UserPerCent = 41.54f }),
                (new VideoAdapter { strDate = "2010-05", UserNum = 102.94f, TerminalCount = 52, UserPerCent = 51.53f }),
                (new VideoAdapter { strDate = "2010-06", UserNum = 107.06f, TerminalCount = 64, UserPerCent = 53.59f }),
                (new VideoAdapter { strDate = "2010-07", UserNum = 145.14f, TerminalCount = 78, UserPerCent = 72.65f }),
                (new VideoAdapter { strDate = "2010-08", UserNum = 153.34f, TerminalCount = 88, UserPerCent = 76.75f })
            };
        }

        private VideoAdapterCollection GetDataSourceWap()
        {
            return new VideoAdapterCollection()
            {
                (new VideoAdapter { strDate = "2010-02", UserNum = 107.09f, TerminalCount = 35, UserPerCent = 30.03f }),
                (new VideoAdapter { strDate = "2010-03", UserNum = 216.27f, TerminalCount = 61, UserPerCent = 60.65f }),
                (new VideoAdapter { strDate = "2010-04", UserNum = 235.48f, TerminalCount = 72, UserPerCent = 66.04f }),
                (new VideoAdapter { strDate = "2010-05", UserNum = 263.2f, TerminalCount = 91, UserPerCent = 73.81f }),
                (new VideoAdapter { strDate = "2010-06", UserNum = 350.48f, TerminalCount = 106, UserPerCent = 98.29f }),
                (new VideoAdapter { strDate = "2010-07", UserNum = 350.48f, TerminalCount = 107, UserPerCent = 98.29f }),
                (new VideoAdapter { strDate = "2010-08", UserNum = 350.52f, TerminalCount = 122, UserPerCent = 98.3f })
            };
        }


        private VideoAdapterCollection GetDataSourceFirst(VideoAdapterCollection vac)
        {
            VideoAdapterCollection retVac = new VideoAdapterCollection();
            for (int i = 0; i < vac.Count; i++)
            {
                retVac.Add(new VideoAdapter() { strDate = vacSourClinet[i].strDate });
            }
            VideoAdapterCopy(vac,retVac, 0);
            return retVac;
        }

        //private void SingleBinding(int index)
        //{
        //    for (int i = 0; i < 2; i++)
        //    {
        //        vacClinet[index].strDate = vacClinetSour[index].strDate;
        //        vacClinet[index].UserNum += vacClinetSour[index].UserNum / 2;
        //        vacClinet[index].TerminalCount += vacClinetSour[index].TerminalCount / 2;
        //        vacClinet[index].UserPerCent += vacClinetSour[index].UserPerCent / 2;
        //        BindingChart(mcChartClient, vacClinet);
        //    }
        //    VideoAdapterCopy(index);
        //}

    }
}
